<?php
/**
*
* @package {PACKAGENAME}
* @version $Id: $
* @copyright (c) 2008 Alejandro Darío Simi
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
if (!defined('__FORUMTOOLS_PHP__')) {
define('__FORUMTOOLS_PHP__', ' ');

/**
 * @ignore
 */
include '../config.php';

/**
 * TODO
 */
class ForumTools
{
	private static	$vInstance = null;

	private	$configs;
	/**
	 * Constructor.
	 */
	private function __construct()
	{
		$this->configs = array();
	}

	/**
	 * string get($key)
	 * this method returns any configurations param.
	 * @param $key is the key-name for the configuration param to request.
	 * @return returns the string value or false if it's not found.
	 */
	public static function get($key)
	{
		ForumTools::load();
		$me = ForumTools::instance();
		return (isset($me->configs[$key])) ? $me->configs[$key] : false;
	}

	/**
	 * void load()
	 * This method tries to load all the configurations params.
	 */
	private static function load()
	{
		global	$db;
		global	$table_prefix;

		$me = ForumTools::instance();

		if (!count($me->configs))
		{
			connect_database();

			$sql = "SELECT	*
					FROM	{$table_prefix}config";

			$res = $db->sql_query($sql);
			while ($row = $db->sql_fetchrow($res))
			{
				$me->configs[$row['config_name']] = $row['config_value'];
			}
			$db->sql_freeresult($res);
		}
	}

	/**
	 * ForumTools instance()
	 * returns the singleton instance for ForumTools. If it doesn't exists,
	 * it will be created here.
	 */
	private static function instance()
	{
		if (!ForumTools::$vInstance)
		{
			ForumTools::$vInstance = new ForumTools();
		}
		return ForumTools::$vInstance;
	}

	/**
	 * string sitename()
	 * @return returns the site-name.
	 */
	public static function sitename()
	{
		return ForumTools::get('sitename');
	}

	/**
	 * string description()
	 * @return returns the site description.
	 */
	public static function description()
	{
		return ForumTools::get('site_desc');
	}

	/**
	 * string link()
	 * @return returns the complete link to refer the forum.
	 */
	public static function link()
	{
		$out = ForumTools::get('server_protocol');
		$out.= ForumTools::get('server_name');
		$aux = ForumTools::get('server_port');
		$out.= ($aux && $aux != '80') ? ":$aux" : '';
		$aux = ForumTools::get('script_path');
		$out.= $aux ? $aux : '/';
		return $out;
	}
	
	/**
	 * string adapt_tildes()
	 * @param $data It is the string to be parsed as a BB string.
	 * @return the string without encoding problems.
	 */
	public static function adapt_tildes($data)
	{
		$data = str_replace('Á', '&Aacute;', $data);
		$data = str_replace('É', '&Eacute;', $data);
		$data = str_replace('Í', '&Iacute;', $data);
		$data = str_replace('Ó', '&Oacute;', $data);
		$data = str_replace('Ú', '&Uacute;', $data);
		$data = str_replace('á', '&aacute;', $data);
		$data = str_replace('é', '&eacute;', $data);
		$data = str_replace('í', '&iacute;', $data);
		$data = str_replace('ó', '&oacute;', $data);
		$data = str_replace('ú', '&uacute;', $data);
		$data = str_replace('Ä', '&Auml;', $data);
		$data = str_replace('Ë', '&Euml;', $data);
		$data = str_replace('Ï', '&Iuml;', $data);
		$data = str_replace('Ö', '&Ouml;', $data);
		$data = str_replace('Ü', '&Uuml;', $data);
		$data = str_replace('ä', '&auml;', $data);
		$data = str_replace('ë', '&euml;', $data);
		$data = str_replace('ï', '&iuml;', $data);
		$data = str_replace('ö', '&ouml;', $data);
		$data = str_replace('u', '&uuml;', $data);
		$data = str_replace('Ñ', '&Ntilde;', $data);
		$data = str_replace('ñ', '&ntilde;', $data);

		return $data;
	}
	
	/**
	 * string adapt_bb_2_html()
	 * @param $data It is the string to be parsed as a BB string.
	 * @param $flags options array.
	 * @return the string as a HTML string.
	 */
	public static function adapt_bb_2_html($data, $flags=null)
	{
		$data = preg_replace('#\[b\]((.*?))\[/b\]#s', '<b>$1</b>', $data);
		$data = preg_replace('#\[i\]((.*?))\[/i\]#s', '<i>$1</i>', $data);
		$data = preg_replace('#\[u\]((.*?))\[/u\]#s', '<u>$1</u>', $data);
		$data = preg_replace('#\[center\]((.*?))\[/center\]#s', '<center>$1</center>', $data);
		$data = preg_replace('#\[size=([^\[]+?)\](.*?)\[/size\]#s', '<span style="size:$1">$2</span>', $data);
		$data = preg_replace('#\[url=([^\[]+?)\](.*?)\[/url\]#s', '<a href="$1">$2</a>', $data);
		$data = preg_replace('#\[url\]((.*?))\[/url\]#s', '<a href="$1">$1</a>', $data);
		$data = preg_replace('#\[email=([^\[]+?)\](.*?)\[/email\]#s', '<a href="mailto:$1">$2</a>', $data);
		$data = preg_replace('#\[email\]((.*?))\[/email\]#s', '<a href="mailto:$1">$1</a>', $data);
		$data = preg_replace('#\[color=([^\[]+?)\](.*?)\[/color\]#s', '<span style="color:$1">$2</span>', $data);
		$data = preg_replace('#\[img\]((.*?))\[/img\]#s', '<img src="$1"/>', $data);
		while(strpos($data, '[list]') && strpos($data, '[/list]'))
		{
			$data = preg_replace('#\[list\]((.*?))\[/list\]#s', '<ul>$1</ul>', $data);
		}
		$data = preg_replace('#\[\*\]((.*?))#s', '<li>$1</li>', $data);
		$data = preg_replace('#\[quote\]((.*?))\[/quote\]#s', '<blockquote style="left:auto;right:auto;padding:2px 2px 2px 2px;border:1px solid #999;">$1</blockquote>', $data);
		$data = preg_replace('#\[youtube=([^\[]+?)\](.*?)\[/youtube\]#s', '<a href="$1">YouTube[$2]</a>', $data);
		$data = preg_replace('#\[youtube-list=([^\[]+?)\](.*?)\[/youtube-list\]#s', '<a href="$1">YouTube[$2]</a>', $data);
		$data = preg_replace('#\[google-doc=([^\[]+?)\](.*?)\[/google-doc\]#s', '<a href="$1">GoogleDoc[$2]</a>', $data);
		$data = str_replace("\n", "<br/>", $data);

		return $data;
	}

	/**
	 * string adapt_string()
	 * @param $data
	 * @param $isBB
	 * @return returns the complete link to refer the forum.
	 */
	public static function adapt_string($data, $isBB=false)
	{
		if ($isBB && is_array($data))
		{
			$flags['display_uid'] = $data['bbcode_uid'];
			$flags['display_bitfield'] = $data['bbcode_bitfield'];
			$flags['display_flags'] = '';
//			$flags['display_flags'] = (($data['enable_bbcode']) ? OPTION_FLAG_BBCODE : 0) +
//							(($data['enable_smilies']) ? OPTION_FLAG_SMILIES : 0) +
//							(($data['enable_magic_url']) ? OPTION_FLAG_LINKS : 0);
			$data = $data['post_text'];
			decode_message($data, $flags['display_uid']);
			$data = ForumTools::adapt_bb_2_html($data, $flags);
		}

		while(strpos($data, '  '))
		{
			$data = str_replace('  ', '&nbsp;&nbsp;', $data);
		}

		/*
		 * Readapting tildes.
		 */
		$data = ForumTools::adapt_tildes($data);

		/*
		 * Before '&' replaces we need to replace wrong '&#'.
		 */
		$data = str_replace('&#46;', '.', $data);
		$data = str_replace('&#58;', ':', $data);

		$data = str_replace('&', '&amp;', $data);
		$data = str_replace('<', '&lt;', $data);
		$data = str_replace('>', '&gt;', $data);

		return $data;
	}

	public static function is_private($forum_id, $topic_id) {
		global	$private_forums;
		global	$private_topics;

		return (in_array($forum_id, $private_forums) || in_array($topic_id, $private_topics));
	}
	public static function is_protected($forum_id, $topic_id) {
		global	$protected_forums;
		global	$protected_topics;

		return (in_array($forum_id, $protected_forums) || in_array($topic_id, $protected_topics));
	}
};

} // __FORUMTOOLS_PHP__ //
?>
